<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="订单编号" prop="orderNum">
          <el-input
            v-model="queryParams.orderNum"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['medicine:order:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            @click="carList()"
            v-hasPermi="['medicine:order:edit']"
          >采购列表</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['medicine:order:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="订单ID" align="center" prop="orderId" />
        <el-table-column label="订单编号" align="center" prop="orderNum" >
          <template #default="scope">
            <router-link :to="'/medicine/order_detail/detail/' + scope.row.orderId" class="link-type">
                    <span>{{ scope.row.orderNum }}</span>
                 </router-link>
          </template>
        </el-table-column>
        <el-table-column label="制单人" align="center" prop="orderPerson" />
        <el-table-column label="供应商ID" align="center" prop="supplierId" />
        <el-table-column label="订单状态" align="center" prop="orderStatus">
          <template #default="scope">
            <dict-tag :options="order_status" :value="scope.row.orderStatus" />
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['medicine:order:edit']">检验</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改订单信息对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="订单编号" prop="orderNum">
            <el-input v-model="form.orderNum"  readonly="true" />
          </el-form-item>
          <el-form-item label="订单状态" prop="orderStatus">
            <el-select v-model="form.orderStatus" placeholder="请选择订单状态">
              <el-option
                v-for="dict in check_result"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="Order">
  import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/his/order.js";
  import {addCheck} from "@/api/his/check.js";
  const { proxy } = getCurrentInstance();
  const { order_status ,check_result } = proxy.useDict('order_status','check_result');
  const router = useRouter();
  
  const orderList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      orderNum: null,
      orderPerson: null,
      orderStatus: 5,
    },
    rules: {
      orderPerson: [
        { required: true, message: "制单人不能为空", trigger: "change" }
      ],
      supplierId: [
        { required: true, message: "供应商ID不能为空", trigger: "change" }
      ],
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);
  
  /** 查询订单信息列表 */
  function getList() {
    loading.value = true;

    listOrder(queryParams.value).then(response => {
      orderList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset() {
    form.value = {
      orderId: null,
      orderNum: null,
      orderPerson: null,
      supplierId: null,
      orderMoney: null,
      orderStatus: null,
      status: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null,
      remark: null
    };
    proxy.resetForm("orderRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.orderId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  
  /** 新增按钮操作 */
  function handleAdd() {
    router.push("/medicine/order-add/addOrder")
  }
  
  
  /** 修改按钮操作 */
  function carList() {
    router.push("/medicine/car_list/carList")
  }
  
  // 检验时打开对话框
  function handleUpdate(data){
    open.value = true;
    title.value = "检验状态"
    form.value.orderId = data.orderId
    //通过id查订单id
    getOrder(form.value).then(res=>{
    form.value = res.data;
    form.value.orderStatus = null;
  })
  }
  /** 提交按钮 */
  function submitForm() {
        //检验保存检验记录
        const data = ref({
            orderId :form.value.orderId,
            checkResult :form.value.orderStatus,
            checkContent : form.value.remark,
        })
        if (form.value.orderId != null) {
            addCheck(data.value).then(response => {
            proxy.$modal.msgSuccess("操作成功");
            open.value = false;
            getList();
          });
   }
  }
  
  /** 删除按钮操作 */
  function handleDelete(row) {
    const _orderIds = row.orderId || ids.value;
    proxy.$modal.confirm('是否确认删除订单信息编号为"' + _orderIds + '"的数据项？').then(function() {
      return delOrder(_orderIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  
  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('medicine/order/export', {
      ...queryParams.value
    }, `order_${new Date().getTime()}.xlsx`)
  }
  
  getList();
  </script>
  